package com.wsj.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.actions.DispatchAction;

import com.wsj.web.constant.page.PageContent;
import com.wsj.web.subclass.function.ProductsParamentObj;
import com.wsj.web.vo.homeSite.UserBase;

public class BaseAction extends DispatchAction {

    //日志
    public Log logger=LogFactory.getLog(this.getClass());

    /***
     * 弹出信息设置
     */
    public void setPopMsg(HttpServletRequest request,String msgName,String msg)
    {
        request.setAttribute(msgName,"<script type=\"text/javascript\" language=\"javascript\">alert(\""+msg+"\")</script>");
        
        logger.debug("\n\n"+msg);
    }
    
   
    /********
     * 从session中获取用户的对象
     */
    public UserBase getUserBaseFromSession(HttpServletRequest request){
    	
    	 HttpSession  session = request.getSession();
    	 if(session!=null){
    		 UserBase userBase =  (UserBase) session.getAttribute("UserBase");
    		 return userBase;
    	 }
    	 return null;
    	
    }
    
    /********
     * 检查当前用户是否是管理员
     */
   public boolean checkIsAdmin(HttpServletRequest request){
	   UserBase  userBase = getUserBaseFromSession(request);
	   if(userBase!=null){
		   return userBase.isAdmin();
	   }
	   return false;
   }
    
    
   /*******
    * 获取当前用户名
    */
   public String getUserNameFromSession(HttpServletRequest request){
	   UserBase  userBase = getUserBaseFromSession(request);
	   if(userBase!=null){
		   return userBase.getUserName();
	   }
	   return "";
   }
   
   /******
    * 获取当前用户id
    */
   public long getUserIdFromSession(HttpServletRequest request){
	   UserBase  userBase = getUserBaseFromSession(request);
	   if(userBase!=null){
		   return userBase.getUserId();
	   }
	   return 0;
   }
    
    
    /*****
     * 获得分页参数  [0]页码,[1]页显示数
     */
    
    public Integer[] loadPageParament(HttpServletRequest request)
    {
        
        Integer[] retI=new Integer[2];
        int page=PageContent.pageValue;
        int pageSize=PageContent.pageSizeValue;
        
       
       
        ProductsParamentObj parament=new ProductsParamentObj();
        
        String[] ret=parament.dealWithParaments(request,new String[]{
                PageContent.page,
                PageContent.pageSize,
                PageContent.doit,
                PageContent.backPage,
                PageContent.nextPage
                });
        
        String doit="";
        
        //页码
        if(ret[0]!=null)
        {
            page=Integer.parseInt(ret[0]);
        }
        
        
        //显示数
        if(ret[1]!=null)
        {
            pageSize=Integer.parseInt(ret[1]);
        }  
        
        
        //跳转
        if(ret[2]!=null)
        {
            doit=ret[2];
        }
        
        //上一页
        if(doit!=null&&doit.equals(PageContent.backPage)&&ret[3]!=null&&!ret[3].equals(""))
        {
            page=Integer.parseInt(ret[3]);
        }
        
        //下一页
        if(doit!=null&&doit.equals(PageContent.nextPage)&&ret[4]!=null&&!ret[4].equals(""))
        {
            page=Integer.parseInt(ret[4]);
        }
        
        retI[0]=page;
        retI[1]=pageSize;
        
        return retI;
    }


	public Log getLogger() {
		return logger;
	}


	public void setLogger(Log logger) {
		this.logger = logger;
	}
    
	
}
